import { BaseComp } from '../basecomp';
import { COMPTYPE, CONSTKEY, convertArgToArray } from '../utils';

/**
 * 基于easyui's dialog的复式弹出窗口组件
 */
export class EasyuiDialogs extends BaseComp {
    public paint(options: any) { 
    }
    public list:KnockoutObservableArray<any>;
    public bindContext:any;
    constructor(params, componentConfig){
        super(COMPTYPE.norm);
        this.name = 'dialogs';
        this.bindContext = ko.contextFor(componentConfig.element);
        this.list = ko.observableArray(); 
    }
    public repaint(options: any) {
    }
    /**
     * 添加弹出窗口
     * @param params 
     */
    public addDialog(params){
        let options = params || {};
        let that = this;
        let key = 'dialog' + ko.unwrap(this.list).length;
        options.onClose = ((key)=>{ 
            return function(){  
                that.removeDialog(key);
            }
        })(key);
        this.list.push({
            name:'ko-dialog',
            params:{
                options: options
            },
            key: key
        });
    }
    /**
     * 移除已弹出的窗口
     * @param key 需要移除的窗口的编号
     */
    public removeDialog(key:string){
        this.list.remove((item)=>{
            return item.key === key;
        });
    } 
    static getTemplate(){
        return '<div data-bind="foreach:{data:list}" ><div data-bind="component:$data,attr:{id:(\'dialogs_\' + $index())}" ></div></div>'
    }
}